function handle_tables() {
    if (!jQuery().dataTable) {
        return;
    }

    $('#container_table').dataTable({
        "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "sPaginationType": "bootstrap",
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page",
            "oPaginate": {
                "sPrevious": "Prev",
                "sNext": "Next"
            }
        },
        "aoColumnDefs": [{
            'bSortable': false,
            'aTargets': [0]
        }]
    });
}



function handle_container_actions() {
    var notification_area = $("#notification");

    $(".state_button").click(function () {
        var current_td = $(this).parent().parent();
        var container_name = current_td.find(".container_name").html();
        var state_button = current_td.find(".state_button");

        if (state_button.hasClass("running")) {
            $.ajax({
                url: "/docker/container/stop/" + container_name,
                type: "GET",
                dataType: "json",
                success: function () {
                    state_button.toggleClass("running btn-warning btn-success");
                    state_button.html('<i class="icon-play"></i> Start</button>');
                    current_td.find(".container_status").html("N/A");

                    notification_area.removeClass("hidden");
                    notification_area.html(
                        '<button class="close" data-dismiss="alert">×</button> \
                        Container <strong>' + container_name + '</strong> has been stopped.</div>'
                        );
                }
            });
        }
        else {
            $.ajax({
                url: "/docker/container/start/" + container_name,
                type: "GET",
                dataType: "json",
                success: function () {
                    state_button.toggleClass("running btn-warning btn-success");
                    state_button.html('<i class="icon-pause"></i> Stop</button>');
                    current_td.find(".container_status").html("Up 1 second");

                    notification_area.removeClass("hidden");
                    notification_area.html(
                        '<button class="close" data-dismiss="alert">×</button> \
                        Container <strong>' + container_name + '</strong> has been started.</div>'
                        );
                }
            });
        }
    });

    $(".delete_button").click(function () {
        var current_td = $(this).parent().parent();
        var container_name = current_td.find(".container_name").html();
        $.ajax({
            url: "/docker/container/delete/" + container_name,
            type: "GET",
            dataType: "json",
            success: function () {
                current_td.remove();
                notification_area.removeClass("hidden");
                notification_area.html(
                    '<button class="close" data-dismiss="alert">×</button> \
                    Container <strong>' + container_name + '</strong> has been deleted.'
                    );
            }
        });
    });
}



$(document).ready(function () {
    handle_tables();
    handle_container_actions();
});